<?php
/**
 * 申请异动
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Flow_Transfer");
apf_require_class("Bll_Hr_Property");

class Flow_ApplyTransferController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        //检测查看权限
        if(!$this->_check_superior_auth($params)){
            $this->show_tips('ERROR_AUTH');
        }
        //是否带flow_id，有则是编辑状态
        $flow_id = isset($params['flow_id']) ? intval($params['flow_id']) : 0;
        $bll_tr = new Bll_Flow_Transfer();

        //检测当前阶段是否可以编辑
        $edit_flag = 1;
        if($flow_id){
            $transfer_info = $bll_tr->get_transfer_by_flow($flow_id);
            if(!$transfer_info){
                $this->show_tips('ERROR_PARAM');
            }else{
                $transfer_info['before_info'] = explode(',', $transfer_info['before_info']);
            }

            $bll_flow = new Bll_Flow_Info();
            $flow_info = $bll_flow->find_by_id($flow_id);
            if($flow_info['now_status'] == 1){
                if(1 == $flow_info['flow_stage']){
                    $edit_flag = 1;
                }else{
                    $edit_flag = 0;
                }
            }else{
                $edit_flag = 0;
            }
            //在这里
            $rrs = $this->_go_bak_apply_transfer($flow_info, $transfer_info['apply_user_id']);
            if($rrs){
                $edit_flag = 1;
            }
        }else{//默认写请申请人名
            $transfer_info['new_super_uid'] = $this->user_info['user_id'];
            $transfer_info['super_name'] = $this->user_info['user_name'];
        }

        if($this->request->is_post_method()){
            //如果不能编辑则报错
            if(!$edit_flag){
                $this->show_tips('ERROR_AUTH');
            }
            if(!$this->_check_params($params)){
                $this->show_tips('ERROR_PARAM');
            }
            //如果是添加，则检测是否有主管
            if(!$flow_id){
                //检测父级是否存在
                $bll_hr = new Bll_Hr_OutInterface();
                $parent_info = $bll_hr->get_under_leader(intval($params['user_id']));
                if(!$parent_info){
                    $this->show_tips('ERROR_PARENT');
                }
            }
            if(isset($params['transfer_type']) || !empty($params['job_id'])){//如果填写了转岗字段，则进入转岗流程。否则进入薪资调整流程
                $flow_type =  Const_Flow::FLOW_TYPE_TRANSFER ;//流程类型
                $now_flow = Const_Flow::FLOW_TRANSFER_1;//当前操作阶段
            }else{
                $flow_type = Const_Flow::FLOW_TYPE_MONEY;
                $now_flow = Const_Flow::FLOW_MONEY_1;
            }
            //当前操作ID
            $flow_opt = Const_Flow::PHASE_11;
            /*if(!in_array($flow_opt, $this->_check_auth($params['city_id'],$flow_type,$now_flow))){
                $this->show_tips('ERROR_AUTH');
            }*/
            //如果选择转岗，则转岗类型为1（转岗），否则为2（调薪，绩效）
            $params['flow_type'] = $flow_type;
            $params['apply_user_id'] = $this->user_info['user_id'];
            $params['plan_date'] = date('Ymd', strtotime($params['plan_date']));
            $params['flow_stage'] = $now_flow;
            $params['flow_opt'] = $flow_opt;
            $flow_role_list = array_keys($this->_get_flow_step($params['flow_type'],Const_Flow::FLOW_TRANSFER_2));
            $params['flow_role_list'] = implode(',', $flow_role_list);
            $params['role_type']      = Const_Flow::ROLE_SUPERIOR;
            $params['flow_stage']     = 1;

            if(!$flow_id){
                $re = $bll_tr->add_transfer($params);
            }else{
                $re = $bll_tr->update_transfer($params);
            }

            if($re){
                $nex_url = Flow_Urls::flow_done_list();
                $this->show_tips('OPT_SUCCESS',$nex_url,1);
            }else{
                $this->show_tips('ERROR_APPLY_TRANSFER');
            }
        }else{
            //转岗，调薪类型
            $salary_type = Const_Flow::salary_type();
            $transfer_type = Const_Flow::transfer_type();
            $this->request->set_attribute('salary_type', $salary_type);
            $this->request->set_attribute('transfer_type', $transfer_type);
            //职级信息
            $bll_pro = new Bll_Hr_Property();
            $this->request->set_attribute('p_level',$bll_pro->get_p_degree());
            $this->request->set_attribute('m_level',$bll_pro->get_m_degree());
            $this->request->set_attribute('transfer_info',$transfer_info);
            //如果带flow_id访问，则是编辑，取出相应
            $this->request->set_attribute('edit_flag',$edit_flag);
            return "Flow_ApplyTransfer";
        }
    }
    /**
     * 外挂
     * @param unknown_type $flow_info
     * @param unknown_type $apply_user_id
     * @return boolean
     */
    private function _go_bak_apply_transfer($flow_info,$apply_user_id){
        $ceo_uid = APF::get_instance()->get_config('ceo_uid');
        $bll_hr = new Bll_Hr_OutInterface();
        $parent_info = $bll_hr->get_under_leader($flow_info['user_id']);
        //对ceo或者是有跳级的作特殊展示
        $status = false;
        if(in_array($flow_info['flow_stage'],array(Const_Flow::FLOW_TRANSFER_3,Const_Flow::FLOW_MONEY_3)) && $ceo_uid == $apply_user_id && $this->user_info['user_id']==$ceo_uid){
            $status = true;
        }elseif(in_array($flow_info['flow_stage'],array(Const_Flow::FLOW_TRANSFER_2,Const_Flow::FLOW_MONEY_2)) && $parent_info['user_id']==$apply_user_id && $this->user_info['user_id']==$apply_user_id){
            $status = true;
        }
        return $status;
    }
    /**
     * 检测是否有权先访问
     * @param unknown_type $params
     * @return boolean
     */
    private function _check_superior_auth($params){
        $return = false;

        //判断是否有主管权限
        if($this->user_info['is_allow_transfer']){
            //先判断是否为编辑
            $flow_id = intval($params['flow_id']);
            if(empty($flow_id)){
                $return = true;
            }else{
                $return = $this->_check_applyer($flow_id);
            }
        }
        return $return;
    }
    /**
     * 检查参数
     * @param unknown_type $params
     * @return boolean
     */
    private function _check_params($params){
        $return = false;
        if(!$params['flow_id']){
            //判断被操作员工信息是否完整
            if(empty($params['user_id']) || empty($params['user_name']) || empty($params['user_code']) || empty($params['city_id']) || empty($params['plan_date'])){
                return $return;
            }
            $bll = new Bll_Hr_OutInterface();
            $real_user_info = $bll->get_user_base_info($params['user_id']);
            //检测被办理人信息是否正确
            if((trim($params['user_name']) != $real_user_info['user_name']) || (intval($params['city_id']) != $real_user_info['work_city_id'])){
                return $return;
            }
        }
        //判断职位、薪资、职级，是否至少填写一项
        if(empty($params['job_id']) && empty($params['transfer_type']) && empty($params['salary_type']) && empty($params['p_level']) && empty($params['m_level'])){
            return $return;
        }
        //判断岗位异动类型是否存在
        $transfer_type = Const_Flow::transfer_type();//岗位异动类型
        if(isset($params['transfer_type']) && !array_key_exists($params['transfer_type'], $transfer_type)){
            return $return;
        }
        //薪资异动类型
        $salary_type = Const_Flow::salary_type();
        if(isset($params['salary_type'])){
            if(!array_key_exists($params['salary_type'], $salary_type)){
                return $return;
            }
        }
        //检测时间格式
        if(!preg_match('/^[0-9]{4}\-?[0-9]{2}\-?[0-9]{2}$/',$params['plan_date'])){
            return $return;
        }
        return true;

    }
}
